package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import entity.Person;

public class ReadXLSX {
	public List<Person> getXlsxData(File file) {
		InputStream is;
		List<Person> list = new ArrayList<Person>();
		try {
			is=new FileInputStream(file);
			XSSFWorkbook xssfWorkbook=new XSSFWorkbook(is);
			//获取工作簿
			for (int sheetNum = 0; sheetNum < xssfWorkbook.getNumberOfSheets(); sheetNum++) {
				XSSFSheet xssfSheet=xssfWorkbook.getSheetAt(sheetNum);
				if (xssfSheet==null) {
					continue;
				}
				//获取行
				for (int rowNum = 0; rowNum <=xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow=xssfSheet.getRow(rowNum);
					if (xssfRow==null) {
						continue;
					}
					
					Person result=getDataFromCell(xssfRow);
					
					list.add(result);
				
				}
			}
		}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("失败");
		}
		return list;
	
	}
	public Person getDataFromCell(XSSFRow xssfRow) {
		Person p =new Person();
		XSSFCell cell;
		int cellNum=0;
		//获取名字
		cell=xssfRow.getCell(cellNum++);
		p.setName(cell.getStringCellValue());
		
		cell=xssfRow.getCell(cellNum++);
		p.setDepartment(cell.getStringCellValue());
		
		cell=xssfRow.getCell(cellNum++);
		p.setAge((int) cell.getNumericCellValue());
		
		return p;
	}
	
}
